<?php

namespace backend\controllers;

use Yii;
use backend\models\Stock;
use backend\models\StockSearch;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * StockController implements the CRUD actions for Stock model.
 */
class StockController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    public function beforeAction($action)
    {
        //如果未登录，则直接跳转
        if(Yii::$app->user->isGuest){
            return $this->redirect(['/site/login']);
        }

        $action = Yii::$app->controller->action->id;
        $controller = Yii::$app->controller->id;
        if(\Yii::$app->user->can('stock')){
            return true;
        }else{

            throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
        }
    }
    /**
     * Lists all Stock models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new StockSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Stock model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Stock model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Stock();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Stock model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Stock model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Stock model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Stock the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Stock::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionSelectItem(){
        $itemId = Yii::$app->request->post("itemId");
       // var_dump($itemId);
        $model = new Stock();
        $material = $this->formatOption($model->getUsedMaterial($itemId));
        $fabric = $this->formatOption($model->getUsedFabric($itemId));
        $size = $this->formatOption($model->getUsedSize($itemId));

        return json_encode([
            'material' => $material,
            'fabric' => $fabric,
            'size' => $size
        ]);
    }
    public function actionSelectFabric(){
        $itemId = Yii::$app->request->post("itemId");
        $fabricId = Yii::$app->request->post("fabricId");
        $model = new Stock();
        $material = $this->formatOption($model->getUsedMaterial($itemId, $fabricId));
        echo json_encode([
            'material' => $material,
        ]);
    }
    public function actionSelectMaterial(){
        $itemId = Yii::$app->request->post("itemId");
        $materialId = Yii::$app->request->post("materialId");
        $model = new Stock();
        $fabric = $this->formatOption($model->getUsedFabric($itemId, $materialId));
        echo json_encode([
            'fabric' => $fabric,
        ]);
    }
    public function formatOption($result){
        $option = '';
        if ($result) {
            foreach ($result as $key => $value) {
                $option .= '<option value="' . $key . '">' . $value . '</option>';
            }
        } else {
            $option = '<option value="0">暂无</option>';
        }
        return $option;
    }

    protected function getStockModel(){
        return new Stock();
    }
}
